package com.naiterui.ehp.ps.schedule.task;

import cn.hutool.core.map.MapUtil;
import java.util.Map;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.ps.schedule.vo.ScheduleJobVO;

/**
 * 通用Task
 *
 * @author Gaoll mudou@naiterui.com * @since HZD-1.0.0
 */
@DisallowConcurrentExecution
public class CommonTask implements Job {

    public static final String SCHEDULEJOBVO_NAME = "scheduleJob";

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonTask.class);

    @Override
    public void execute(JobExecutionContext context) {
        ScheduleJobVO scheduleJobVO = (ScheduleJobVO) context.getMergedJobDataMap().get(SCHEDULEJOBVO_NAME);
        Map<String, String> parameter = scheduleJobVO.getParameter();
        if (MapUtil.isEmpty(parameter)) {
            parameter = Maps.newHashMap();
        }
        try {
            RestHttpUtil.post().url(scheduleJobVO.getUrl()).paramMap(parameter).executeData();
            LOGGER.info("任务调用成功 任务组 = {} 任务名称 = {} 调用地址 = {} 请求参数 = {}", scheduleJobVO.getGroup(), scheduleJobVO.getName(),
                        scheduleJobVO.getUrl(), parameter);
        } catch (Exception e) {
            LOGGER.error("任务调用失败 任务组 = {} 任务名称 = {} 调用地址 = {} 请求参数 = {}", scheduleJobVO.getGroup(), scheduleJobVO.getName(),
                         scheduleJobVO.getUrl(), parameter, e);
        }
    }
}
